//
//  UrlViewController.m
//  XKEmark
//
//  Created by 徐凯 on 2019/3/31.
//  Copyright © 2019 neebel. All rights reserved.
//

#import "UrlViewController.h"

#define UI_IS_IPHONE      [[UIDevice currentDevice].model isEqualToString:@"iPhone"]
//屏幕长度宽度
#define UI_SCREEN_HEIGHT  MAX([UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height)
#define UI_SCREEN_WIDTH   MIN([UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height)

#define UI_IS_IPHONE_X    (UI_IS_IPHONE && ((UI_SCREEN_WIDTH/UI_SCREEN_HEIGHT) < 0.5))
#define Is_iPhoneX UI_IS_IPHONE_X
// 屏幕宽高
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define font_14 14.0


@interface UrlViewController ()
    
    @property(nonatomic,strong)UIWebView *webView;
    @property(nonatomic,strong)UIButton *updateBtn;
    @property (nonatomic, strong) NSString *cUrl;

@end

@implementation UrlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showWeb];
}

-(void) showWeb
    {
        CGFloat top = Is_iPhoneX ? 44 : 20;
        
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, top, SCREEN_WIDTH, SCREEN_HEIGHT - top - 30)];
        _webView.backgroundColor = [UIColor whiteColor];
        //    _webView.delegate = self;
        _webView.scalesPageToFit = YES;
        NSURL* urlAddress = [NSURL URLWithString:self.URL];
        NSURLRequest* request = [NSURLRequest requestWithURL:urlAddress];
        [_webView loadRequest:request];
        [self.view addSubview:_webView];
        
        CGFloat b = 44;
        if(Is_iPhoneX)
        {
            b = 44 + 30;
        }
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@0);
            make.width.mas_equalTo(UI_SCREEN_WIDTH);
            make.height.equalTo(self.view.mas_height).offset(-b);
        }];
        [self initSetBtn];
    }

    
    //前进 后退 刷新 取消
-(void)initSetBtn{
    CGFloat b = 44;
    if(Is_iPhoneX)
    {
        b = 44 + 30;
    }
    UIView *bageView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - b, SCREEN_WIDTH, b)];
    bageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bageView];
    //顶线
    UIView *topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    topLineView.backgroundColor = [UIColor grayColor];
    [bageView addSubview:topLineView];
    
    //Home
    UIButton *homeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.2, 44)];
    [homeBtn setTitle:@"首页" forState:(UIControlStateNormal)];
    [homeBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    homeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [homeBtn addTarget:self action:@selector(homeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:homeBtn];
    
    //前进
    UIButton *goBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.4, 0, SCREEN_WIDTH*0.2, 44)];
    [goBtn setImage:[UIImage imageNamed:@"hou"] forState:UIControlStateNormal];
    goBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [goBtn addTarget:self action:@selector(goBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:goBtn];
    //后退
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.2, 0, SCREEN_WIDTH*0.2, 44)];
    [backBtn setImage:[UIImage imageNamed:@"qian"] forState:UIControlStateNormal];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:backBtn];
    //刷新
    _updateBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.6, 0, SCREEN_WIDTH*0.2, 44)];
    [_updateBtn setImage:[UIImage imageNamed:@"xin"] forState:UIControlStateNormal];
    _updateBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [_updateBtn addTarget:self action:@selector(updateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:_updateBtn];
    //取消
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.8, 0, SCREEN_WIDTH*0.2, 44)];
    [cancelBtn setImage:[UIImage imageNamed:@"guan"] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:cancelBtn];
}
    
    
-(void) homeBtnClick
    {
        if(_cUrl.length > 0)
        {
            NSURL* urlAddress = [NSURL URLWithString:_cUrl];
            NSURLRequest* request = [NSURLRequest requestWithURL:urlAddress];
            [_webView loadRequest:request];
        }
        
    }
    
    //前进
-(void)goBtnClick{
    if (self.webView.canGoForward) {
        [self.webView goForward];
    }
}
    //后退
-(void)backBtnClick{
    [self.webView goBack];
}
    //刷新
-(void)updateBtnClick{
    [UIView animateWithDuration:0.3 animations:^{
        [_updateBtn setTransform:CGAffineTransformRotate(_updateBtn.transform, M_PI)];
    }];
    [self.webView reload];
}
    //取消
-(void)cancelBtnClick{
    if (self.webView.loading) {
        [self.webView stopLoading];
    }
}
@end
